package com.ccb.sc.common.controller;

import com.ccb.sc.auth.JWTHelper;
import com.ccb.sc.constant.AppConst;
import com.ccb.sc.common.service.ISmsService;
import com.ccb.sc.member.mapper.AppMemberMapper;
import com.ccb.sc.member.service.AppMemberService;
import com.ccb.sc.common.bind.annotation.Limit;
//psl import com.ccb.sc.common.bind.exception.BizException;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.code.BizCodeFace;
import com.ccb.sc.common.code.ErrorCode;
import com.ccb.sc.common.constant.CommonRedisKey;
import com.ccb.sc.common.constant.RegexPatterns;
import com.ccb.sc.common.modules.member.entity.Member;
import com.ccb.sc.common.modules.member.enums.MemberStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ken
 * @version 1.0
 * @date 2020-09-23
 */
@RestController
@RequestMapping("/app/sms")
@Api(value = "发送短信管理", tags = "发送短信管理")
public class SmsController {

    private AppMemberMapper memberMapper;

    /**
     * 用户登录短信模板名
     */
    private final static String LOGIN_SMS_TEMPLATE = "user_login_sms_template";

    /**
     * 用户注册短信模板名
     */
    private final static String REGISTER_SMS_TEMPLATE = "user_register_sms_template";

    /**
     * 用户忘记密码短信模板名
     */
    private final static String FORGET_PASSWORD_SMS_TEMPLATE = "user_forget_password_sms_template";

    private AppMemberService memberService;
    private ISmsService smsService;

    @Limit(period = 10, count = 1)
    @PostMapping("/login")
    @ApiOperation("发送登录注册短信验证码")
    public ResBody<?> login(@Valid @RequestParam String phone) {
        Assert.isTrue(phone.matches(RegexPatterns.MOBILE_PHONE_NUMBER_PATTERN), "请输入正确的手机号格式");
        this.smsService.sendCode(LOGIN_SMS_TEMPLATE, phone, String.format(CommonRedisKey.LOGIN_CODE, phone), 5 * 60);
        return ResBody.success("发送成功");
    }

    @Limit(period = 10, count = 1)
    @PostMapping("/register")
    @ApiOperation("发送注册短信验证码")
    public ResBody<?> register(@Valid @RequestParam String phone) {
        Assert.isTrue(phone.matches(RegexPatterns.MOBILE_PHONE_NUMBER_PATTERN), "请输入正确的手机号格式");
        Member member = this.memberService.findByPhone(phone);
        if (member != null) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.ACCOUNT_EXISTIS).message("手机号已注册"));
        }
        this.smsService.sendCode(REGISTER_SMS_TEMPLATE, phone, String.format(CommonRedisKey.REGISTER_CODE, phone), 5 * 60);
        return ResBody.success("发送成功");
    }

    @Limit(period = 10, count = 1)
    @PostMapping("/forget-pwd")
    @ApiOperation(value = "忘记密码发送短信验证码", response = String.class)
    public ResBody<?> forgetPassword(@Valid @RequestParam String phone) {
        this.validPhone(phone);

        Map<String, String> claims = new HashMap<>(2);
        claims.put("phone", phone);
        claims.put("aud", AppConst.PREFIX_TOKEN_TYPE);
        String sign =JWTHelper.sign(claims, 5*60L);
        this.smsService.sendCode(FORGET_PASSWORD_SMS_TEMPLATE, phone, String.format(CommonRedisKey.FORGET_CODE, phone), 5 * 60);
        return ResBody.success(sign);
    }

    /**
     * 校验手机号是否有效
     * @param phone 手机号
     */
    private void validPhone(String phone) {
        Assert.isTrue(phone.matches(RegexPatterns.MOBILE_PHONE_NUMBER_PATTERN), "请输入正确的手机号格式");
        Member member = this.memberService.findByPhone(phone);
        if (member == null) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.ACCOUNT_NO_EXISTIS));
        }
        if (MemberStatus.DISABLED.equals(member.getStatus())) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.ACCOUNT_LOCKED));
        }
    }

    @Autowired
    public void setMemberService(AppMemberService memberService) {
        this.memberService = memberService;
    }

    @Autowired
    public void setSmsService(ISmsService smsService) {
        this.smsService = smsService;
    }

    @Autowired
    public void setMemberMapper(AppMemberMapper memberMapper) {
        this.memberMapper = memberMapper;
    }
}
